<?php
// ------------------------------------------------------------------------------------
// Bestand:       News, newsController.php
// Versie:        1.4
// Projekt:       Cursus MasterClass van Eduvision
// Datum:         14-07-2008
// Auteur(s):     Ronald Obbens
// Email(s):      ronaldobbens@gmail.com
// Revisie(s):	  1.5 Gebruik maken van de models om gegevens in de database aan te passen en om modelgegevens op te halen
//
// Beschrijving:  deze controller coordineert het gedrag van de models en gegevens stromen
// ------------------------------------------------------------------------------------
//
//

require_once ('XMLparser.php');
require_once ('Newsitem.php');
require_once ('Controller.php');
require_once ('DataBase.php');

class newsController extends Controller {
	private $m_aNewslist = Array ();
	
	public function index() {
	
	}
	
	/**
	 * Get the specified number of News items out of the database.
	 * First get the id's of the last * items. Items take care for the rest of the dataquerys
	 *
	 * @param integer $p_iAantal
	 * @return array with NewsItem Objects
	 */
	public function getLastNewsItems($p_iAantal) {
		
		$output = DataBase::getInstance()->runDirectQuery("SELECT id FROM News ORDER BY date DESC LIMIT " . $p_iAantal . "");
		
		$_aNewslist = array();
		
		foreach($output->getData() as $item) {
			
			$newsitem = new Newsitem();
			$newsitem->setByParentId($item['id']);
			$_aNewslist[] = $newsitem;
		}
		
		return $_aNewslist;
	}
	
	/**
	 * Shows the newsitem paired with the specified newsID
	 *
	 * @param string $p_sParam ,integer newsid/News title
	 */
	public function showItem($p_sParam) {
		$p_sParam = explode ( '/', $p_sParam );
		
		$newsitem = new Newsitem ( );
		$newsitem->setByParentId ( $p_sParam [0] );
		
		parent::assign ( 'title', $newsitem->getTitle () );
		parent::assign ( 'pubdate', date ( 'j-F-Y', $newsitem->getDate () ) );
		parent::assign ( 'author', $newsitem->getAuthor () );
		parent::assign ( 'content', $newsitem->getContent () );
		
		parent::render ( 'newsitem.tpl' );
	}
	
	/**
	 * Updatescript to update the newsdatabase from the rss feeds specified
	 *
	 */
	public function updateNewsFromRSS() {
		$rssfeeds = array ('http://www.medicalnewstoday.com/rss/cystic_fibrosis.xml' );
		
		$xml = new XMLparser ( );
		$DB = DataBase::getInstance();
		
		foreach ( $rssfeeds as $feed ) {
			$xmlitems = $xml->readRSS ( $feed );
			//get the latest date from feed
			
			$oDBoutput = $DB->runDirectQuery ( "SELECT date FROM News WHERE fromfeed = '" . $feed . "' ORDER BY date DESC LIMIT 1" );
			$latestdate = $oDBoutput->getData();
			
			//loop tru the xmlitems
			foreach ( $xmlitems as $item ) {
				//check if an item is not older then the last imported items, ifso insert into news db
				if (strtotime( $item->pubDate ) >= strtotime($latestdate[0])){
				
					$oDBoutput = $DB->runDirectQuery ("insert into News (titel ,author ,link ,content ,fromfeed ,date) values ('" .addslashes($item->title)."',' ','" . addslashes($item->link) . "','" .addslashes($item->description). "','" .$feed."','" .$item->pubDate. "')");
					
				}
			}
		}
	}
}
?>